<?php

namespace app\admin\controller\vbot;

use app\common\controller\Backend;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 微信机器人管理
 *
 * @icon fa fa-circle-o
 */
class Robot extends Backend
{

    /**
     * VbotRobot模型对象
     * @var \app\admin\model\VbotRobot
     */
    protected $model = null;

    protected $multiFields = 'openswitch';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\VbotRobot;
    }

    /*验证机器人配置*/
    public function msg_test()
    {
        $VbotLib = new \addons\vbot\library\VbotLib();

        $webhook = $this->request->only('webhook');

        if (!$webhook['webhook']) {
            $this->error('请输入Hook地址！');
        }

        $data = array(
            'msgtype' => 'text',
            'text'    => [
                'content'               => '这是一条测试消息！',
                'mentioned_mobile_list' => ["@all"]
            ]
        );

        $res = $VbotLib->msgSend($webhook['webhook'], $data);

        if ($res['errcode'] == 0) {
            $this->success('消息发送成功！');
        } else {
            $this->error($res['errmsg'] . '(' . $res['errcode'] . ')');
        }
    }
}